home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / aepack.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  14.7 KB  |  407 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''Tools for use in AppleEvent clients and servers:
  5. conversion between AE types and python types
  6.  
  7. pack(x) converts a Python object to an AEDesc object
  8. unpack(desc) does the reverse
  9. coerce(x, wanted_sample) coerces a python object to another python object
  10. '''
  11. import struct
  12. import string
  13. import types
  14. from string import strip
  15. from types import *
  16. from Carbon import AE
  17. from Carbon.AppleEvents import *
  18. import MacOS
  19. import Carbon.File as Carbon
  20. import StringIO
  21. import aetypes
  22. from aetypes import mkenum, ObjectSpecifier
  23. import os
  24. unpacker_coercions = {
  25.     typeComp: typeFloat,
  26.     typeColorTable: typeAEList,
  27.     typeDrawingArea: typeAERecord,
  28.     typeFixed: typeFloat,
  29.     typeExtended: typeFloat,
  30.     typePixelMap: typeAERecord,
  31.     typeRotation: typeAERecord,
  32.     typeStyledText: typeAERecord,
  33.     typeTextStyles: typeAERecord }
  34. AEDescType = AE.AEDescType
  35. FSSType = Carbon.File.FSSpecType
  36. FSRefType = Carbon.File.FSRefType
  37. AliasType = Carbon.File.AliasType
  38.  
  39. def packkey(ae, key, value):
  40.     if hasattr(key, 'which'):
  41.         keystr = key.which
  42.     elif hasattr(key, 'want'):
  43.         keystr = key.want
  44.     else:
  45.         keystr = key
  46.     ae.AEPutParamDesc(keystr, pack(value))
  47.  
  48.  
  49. def pack(x, forcetype = None):
  50.     '''Pack a python object into an AE descriptor'''
  51.     if forcetype:
  52.         if type(x) is StringType:
  53.             return AE.AECreateDesc(forcetype, x)
  54.         else:
  55.             return pack(x).AECoerceDesc(forcetype)
  56.     
  57.     if x == None:
  58.         return AE.AECreateDesc('null', '')
  59.     
  60.     if isinstance(x, AEDescType):
  61.         return x
  62.     
  63.     if isinstance(x, FSSType):
  64.         return AE.AECreateDesc('fss ', x.data)
  65.     
  66.     if isinstance(x, FSRefType):
  67.         return AE.AECreateDesc('fsrf', x.data)
  68.     
  69.     if isinstance(x, AliasType):
  70.         return AE.AECreateDesc('alis', x.data)
  71.     
  72.     if isinstance(x, IntType):
  73.         return AE.AECreateDesc('long', struct.pack('l', x))
  74.     
  75.     if isinstance(x, FloatType):
  76.         return AE.AECreateDesc('doub', struct.pack('d', x))
  77.     
  78.     if isinstance(x, StringType):
  79.         return AE.AECreateDesc('TEXT', x)
  80.     
  81.     if isinstance(x, UnicodeType):
  82.         data = x.encode('utf16')
  83.         if data[:2] == '\xfe\xff':
  84.             data = data[2:]
  85.         
  86.         return AE.AECreateDesc('utxt', data)
  87.     
  88.     if isinstance(x, ListType):
  89.         list = AE.AECreateList('', 0)
  90.         for item in x:
  91.             list.AEPutDesc(0, pack(item))
  92.         
  93.         return list
  94.     
  95.     if isinstance(x, DictionaryType):
  96.         record = AE.AECreateList('', 1)
  97.         for key, value in x.items():
  98.             packkey(record, key, value)
  99.         
  100.         return record
  101.     
  102.     if type(x) == types.ClassType and issubclass(x, ObjectSpecifier):
  103.         return AE.AECreateDesc('type', x.want)
  104.     
  105.     if hasattr(x, '__aepack__'):
  106.         return x.__aepack__()
  107.     
  108.     if hasattr(x, 'which'):
  109.         return AE.AECreateDesc('TEXT', x.which)
  110.     
  111.     if hasattr(x, 'want'):
  112.         return AE.AECreateDesc('TEXT', x.want)
  113.     
  114.     return AE.AECreateDesc('TEXT', repr(x))
  115.  
  116.  
  117. def unpack(desc, formodulename = ''):
  118.     '''Unpack an AE descriptor to a python object'''
  119.     t = desc.type
  120.     if unpacker_coercions.has_key(t):
  121.         desc = desc.AECoerceDesc(unpacker_coercions[t])
  122.         t = desc.type
  123.     
  124.     if t == typeAEList:
  125.         l = []
  126.         for i in range(desc.AECountItems()):
  127.             (keyword, item) = desc.AEGetNthDesc(i + 1, '****')
  128.             l.append(unpack(item, formodulename))
  129.         
  130.         return l
  131.     
  132.     if t == typeAERecord:
  133.         d = { }
  134.         for i in range(desc.AECountItems()):
  135.             (keyword, item) = desc.AEGetNthDesc(i + 1, '****')
  136.             d[keyword] = unpack(item, formodulename)
  137.         
  138.         return d
  139.     
  140.     if t == typeAEText:
  141.         record = desc.AECoerceDesc('reco')
  142.         return mkaetext(unpack(record, formodulename))
  143.     
  144.     if t == typeAlias:
  145.         return Carbon.File.Alias(rawdata = desc.data)
  146.     
  147.     if t == typeBoolean:
  148.         return struct.unpack('b', desc.data)[0]
  149.     
  150.     if t == typeChar:
  151.         return desc.data
  152.     
  153.     if t == typeUnicodeText:
  154.         return unicode(desc.data, 'utf16')
  155.     
  156.     if t == typeEnumeration:
  157.         return mkenum(desc.data)
  158.     
  159.     if t == typeFalse:
  160.         return 0
  161.     
  162.     if t == typeFloat:
  163.         data = desc.data
  164.         return struct.unpack('d', data)[0]
  165.     
  166.     if t == typeFSS:
  167.         return Carbon.File.FSSpec(rawdata = desc.data)
  168.     
  169.     if t == typeFSRef:
  170.         return Carbon.File.FSRef(rawdata = desc.data)
  171.     
  172.     if t == typeInsertionLoc:
  173.         record = desc.AECoerceDesc('reco')
  174.         return mkinsertionloc(unpack(record, formodulename))
  175.     
  176.     if t == typeIntlText:
  177.         (script, language) = struct.unpack('hh', desc.data[:4])
  178.         return aetypes.IntlText(script, language, desc.data[4:])
  179.     
  180.     if t == typeIntlWritingCode:
  181.         (script, language) = struct.unpack('hh', desc.data)
  182.         return aetypes.IntlWritingCode(script, language)
  183.     
  184.     if t == typeKeyword:
  185.         return mkkeyword(desc.data)
  186.     
  187.     if t == typeLongInteger:
  188.         return struct.unpack('l', desc.data)[0]
  189.     
  190.     if t == typeLongDateTime:
  191.         (a, b) = struct.unpack('lL', desc.data)
  192.         return (long(a) << 32) + b
  193.     
  194.     if t == typeNull:
  195.         return None
  196.     
  197.     if t == typeMagnitude:
  198.         v = struct.unpack('l', desc.data)
  199.         if v < 0:
  200.             v = 0x100000000L + v
  201.         
  202.         return v
  203.     
  204.     if t == typeObjectSpecifier:
  205.         record = desc.AECoerceDesc('reco')
  206.         if formodulename:
  207.             return mkobjectfrommodule(unpack(record, formodulename), formodulename)
  208.         
  209.         return mkobject(unpack(record, formodulename))
  210.     
  211.     if t == typeQDPoint:
  212.         (v, h) = struct.unpack('hh', desc.data)
  213.         return aetypes.QDPoint(v, h)
  214.     
  215.     if t == typeQDRectangle:
  216.         (v0, h0, v1, h1) = struct.unpack('hhhh', desc.data)
  217.         return aetypes.QDRectangle(v0, h0, v1, h1)
  218.     
  219.     if t == typeRGBColor:
  220.         (r, g, b) = struct.unpack('hhh', desc.data)
  221.         return aetypes.RGBColor(r, g, b)
  222.     
  223.     if t == typeShortFloat:
  224.         return struct.unpack('f', desc.data)[0]
  225.     
  226.     if t == typeShortInteger:
  227.         return struct.unpack('h', desc.data)[0]
  228.     
  229.     if t == typeTargetID:
  230.         return mktargetid(desc.data)
  231.     
  232.     if t == typeTrue:
  233.         return 1
  234.     
  235.     if t == typeType:
  236.         return mktype(desc.data, formodulename)
  237.     
  238.     if t == 'rang':
  239.         record = desc.AECoerceDesc('reco')
  240.         return mkrange(unpack(record, formodulename))
  241.     
  242.     if t == 'cmpd':
  243.         record = desc.AECoerceDesc('reco')
  244.         return mkcomparison(unpack(record, formodulename))
  245.     
  246.     if t == 'logi':
  247.         record = desc.AECoerceDesc('reco')
  248.         return mklogical(unpack(record, formodulename))
  249.     
  250.     return mkunknown(desc.type, desc.data)
  251.  
  252.  
  253. def coerce(data, egdata):
  254.     '''Coerce a python object to another type using the AE coercers'''
  255.     pdata = pack(data)
  256.     pegdata = pack(egdata)
  257.     pdata = pdata.AECoerceDesc(pegdata.type)
  258.     return unpack(pdata)
  259.  
  260.  
  261. def mktargetid(data):
  262.     sessionID = getlong(data[:4])
  263.     name = mkppcportrec(data[4:4 + 72])
  264.     location = mklocationnamerec(data[76:76 + 36])
  265.     rcvrName = mkppcportrec(data[112:112 + 72])
  266.     return (sessionID, name, location, rcvrName)
  267.  
  268.  
  269. def mkppcportrec(rec):
  270.     namescript = getword(rec[:2])
  271.     name = getpstr(rec[2:2 + 33])
  272.     portkind = getword(rec[36:38])
  273.     if portkind == 1:
  274.         ctor = rec[38:42]
  275.         type = rec[42:46]
  276.         identity = (ctor, type)
  277.     else:
  278.         identity = getpstr(rec[38:38 + 33])
  279.     return (namescript, name, portkind, identity)
  280.  
  281.  
  282. def mklocationnamerec(rec):
  283.     kind = getword(rec[:2])
  284.     stuff = rec[2:]
  285.     if kind == 0:
  286.         stuff = None
  287.     
  288.     if kind == 2:
  289.         stuff = getpstr(stuff)
  290.     
  291.     return (kind, stuff)
  292.  
  293.  
  294. def mkunknown(type, data):
  295.     return aetypes.Unknown(type, data)
  296.  
  297.  
  298. def getpstr(s):
  299.     return s[1:1 + ord(s[0])]
  300.  
  301.  
  302. def getlong(s):
  303.     return ord(s[0]) << 24 | ord(s[1]) << 16 | ord(s[2]) << 8 | ord(s[3])
  304.  
  305.  
  306. def getword(s):
  307.     return ord(s[0]) << 8 | ord(s[1]) << 0
  308.  
  309.  
  310. def mkkeyword(keyword):
  311.     return aetypes.Keyword(keyword)
  312.  
  313.  
  314. def mkrange(dict):
  315.     return aetypes.Range(dict['star'], dict['stop'])
  316.  
  317.  
  318. def mkcomparison(dict):
  319.     return aetypes.Comparison(dict['obj1'], dict['relo'].enum, dict['obj2'])
  320.  
  321.  
  322. def mklogical(dict):
  323.     return aetypes.Logical(dict['logc'], dict['term'])
  324.  
  325.  
  326. def mkstyledtext(dict):
  327.     return aetypes.StyledText(dict['ksty'], dict['ktxt'])
  328.  
  329.  
  330. def mkaetext(dict):
  331.     return aetypes.AEText(dict[keyAEScriptTag], dict[keyAEStyles], dict[keyAEText])
  332.  
  333.  
  334. def mkinsertionloc(dict):
  335.     return aetypes.InsertionLoc(dict[keyAEObject], dict[keyAEPosition])
  336.  
  337.  
  338. def mkobject(dict):
  339.     want = dict['want'].type
  340.     form = dict['form'].enum
  341.     seld = dict['seld']
  342.     fr = dict['from']
  343.     if form in ('name', 'indx', 'rang', 'test'):
  344.         if want == 'text':
  345.             return aetypes.Text(seld, fr)
  346.         
  347.         if want == 'cha ':
  348.             return aetypes.Character(seld, fr)
  349.         
  350.         if want == 'cwor':
  351.             return aetypes.Word(seld, fr)
  352.         
  353.         if want == 'clin':
  354.             return aetypes.Line(seld, fr)
  355.         
  356.         if want == 'cpar':
  357.             return aetypes.Paragraph(seld, fr)
  358.         
  359.         if want == 'cwin':
  360.             return aetypes.Window(seld, fr)
  361.         
  362.         if want == 'docu':
  363.             return aetypes.Document(seld, fr)
  364.         
  365.         if want == 'file':
  366.             return aetypes.File(seld, fr)
  367.         
  368.         if want == 'cins':
  369.             return aetypes.InsertionPoint(seld, fr)
  370.         
  371.     
  372.     if want == 'prop' and form == 'prop' and aetypes.IsType(seld):
  373.         return aetypes.Property(seld.type, fr)
  374.     
  375.     return aetypes.ObjectSpecifier(want, form, seld, fr)
  376.  
  377.  
  378. def mkobjectfrommodule(dict, modulename):
  379.     if type(dict['want']) == types.ClassType and issubclass(dict['want'], ObjectSpecifier):
  380.         classtype = dict['want']
  381.         dict['want'] = aetypes.mktype(classtype.want)
  382.     
  383.     want = dict['want'].type
  384.     module = __import__(modulename)
  385.     codenamemapper = module._classdeclarations
  386.     classtype = codenamemapper.get(want, None)
  387.     newobj = mkobject(dict)
  388.     if classtype:
  389.         if not issubclass(classtype, ObjectSpecifier):
  390.             raise AssertionError
  391.         newobj.__class__ = classtype
  392.     
  393.     return newobj
  394.  
  395.  
  396. def mktype(typecode, modulename = None):
  397.     if modulename:
  398.         module = __import__(modulename)
  399.         codenamemapper = module._classdeclarations
  400.         classtype = codenamemapper.get(typecode, None)
  401.         if classtype:
  402.             return classtype
  403.         
  404.     
  405.     return aetypes.mktype(typecode)
  406.  
  407.